<script>
	function makeRead(str,conn){

		if (str=="")
		  {
		  document.getElementById("txtHint").innerHTML="";
		  return;
		  } 
		if (window.XMLHttpRequest)
		  {// code for IE7+, Firefox, Chrome, Opera, Safari
		  xmlhttp=new XMLHttpRequest();
		  }
		else
		  {// code for IE6, IE5
		  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		  }
		xmlhttp.onreadystatechange=function()
		  {
		  if (xmlhttp.readyState==4 && xmlhttp.status==200)
		    {
		    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
		    }
		  }
		xmlhttp.open("GET","changeEmailToRead.php?q="+str+"&f="+conn,true);
		//the above line needs to be changed to fit our seen status
		//What should our command be?
		//How do we format it for open()?

		
		xmlhttp.send();
	}
</script>

<?php
/*
	Copyright (c) 2011 Thinkersmith

	Permission is hereby granted, free of charge, to any person obtaining a 
	copy of this software and associated documentation files (the "Software"), 
	to deal in the Software without restriction, including without limitation 
	the rights to use, copy, modify, merge, publish, distribute, sublicense, 
	and/or sell copies of the Software, and to permit persons to whom the 
	Software is furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in 
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
	THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
	FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
	DEALINGS IN THE SOFTWARE.

*/


/* connect to gmail */


$hostname = '{imap.gmail.com:993/imap/ssl}KANGAROOMAIL';

/* Original assignments
$username = 'ts.gosg@gmail.com';  //this will be the parent's email address
$password = '0pens0urce';         //this will be the parent's password 
*/

$username = $_POST["uEmail"];  //this will be the parent's email address (not hard coded)
$password = $_POST["uPwd"];         //this will be the parent's password (not hard coded)

/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());

/* grab emails */
$emails = imap_search($inbox,'ALL');

/* if emails are returned, cycle through each... */
if($emails) {
  
  /* begin output var */
  $output = '';
  
  /* put the newest emails on top */
  rsort($emails);
  
  /* for every email... */
  foreach($emails as $email_number) {
    
    /* get information specific to this email */
    $overview = imap_fetch_overview($inbox,$email_number,0);
    $message = imap_fetchbody($inbox,$email_number,2,FT_PEEK);
    
    /* output the email header information */

    $theDate = strtotime($overview[0]->date);

    $output.= '<a href = "javascript:void(0)" onclick = "makeRead('.$email_number.', \''.$inbox.'\'); document.getElementById(\'light'.$email_number.'\').style.display=\'block\';document.getElementById(\'fade'.$email_number.'\').style.display=\'block\'">';   

    $output.= '<div class="toggler '.($overview[0]->seen ? 'read' : 'unread').'">';
    //$output.= '<span class="subject">'.$overview[0]->subject.'</span> ';
    $output.= '<span class="from">'.$overview[0]->from.'</span>';
    $output.= '<span class="date">on '.$overview[0]->date.'</span>';
    $output.= '</div>';
    
    /* output the email body */

    //$output.= '<div class="body">'.$message.'</div>';

	$output.= '<div id="light'.$email_number.'" class="white_content">';
	$output.= '<span class="from">'.$overview[0]->from.'</span>';
	$output.= '<span class="date">on '.date('l, F jS, Y g:i a',$theDate).'</span>';
	$output.= '<hr/>';
	$output.= '<div class="body">'.$message.'</div>';
	$output.= '</div>';
	$output.= '<div id="fade'.$email_number.'" class="black_overlay">';
	$output.= '<a href = "javascript:void(0)" onclick = "document.getElementById(\'light'.$email_number.'\').style.display=\'none\';document.getElementById(\'fade'.$email_number.'\').style.display=\'none\'"><div class="X"><img src="images/closingXcopy.png" alt="Close" /></div></a>';
	$output.= '</div>';

  }
  
  echo $output;
} 

/* close the connection */
imap_close($inbox);

?>